package com.ssy.lingxi.contract.serviceimpl;

import cn.hutool.core.date.DateUtil;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.contract.entity.ApplyAmountOrderRecordDO;
import com.ssy.lingxi.contract.model.constant.ApplyAmountOrderStatusEnum;
import com.ssy.lingxi.contract.model.vo.applyamount.request.ApplyAmountOrderRecordPageVO;
import com.ssy.lingxi.contract.model.vo.applyamount.response.ApplyAmountOrderRecordVO;
import com.ssy.lingxi.contract.repository.ApplyAmountOrderRecordRepository;
import com.ssy.lingxi.contract.service.IApplyAmountOrderRecordService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 请款单流转记录接口实现
 * @author 伍将
 * @date 2021/3/10
 * @version 2.0.0
 */
@Service
public class ApplyAmountOrderRecordServiceImpl implements IApplyAmountOrderRecordService {

    @Resource
    private ApplyAmountOrderRecordRepository recordRepository;


    /**
     * 新增外部流转记录
     * @author 伍将
     * @date 2021/3/10
     * @param user: 新增外部流转记录
     * @param applyId: 请款单id
     * @param status: 状态
     * @param statusName: 状态名称
     * @param operate: 操作
     * @param opinion: 意见
     * @return 操作结果
     **/
    @Override
    public Wrapper<Void> add(UserLoginCacheDTO user, Long applyId, Integer status, String statusName, String operate, String opinion) {

        ApplyAmountOrderRecordDO recordDO = new ApplyAmountOrderRecordDO();
        recordDO.setApplyId(applyId);
        recordDO.setUserId(user.getUserId());
        recordDO.setMemberId(user.getMemberId());
        recordDO.setRoleId(user.getMemberRoleId());
        recordDO.setOperator(user.getUserName());
        recordDO.setDepartment(user.getUserOrgName());
        recordDO.setJobTitle(user.getUserJobTitle());
        recordDO.setStatus(status);
        recordDO.setStatusName(ApplyAmountOrderStatusEnum.getMessage(status));
        recordDO.setOperate(operate);
        recordDO.setOperateTime(System.currentTimeMillis());
        recordDO.setOpinion(opinion);

        recordRepository.saveAndFlush(recordDO);

        return Wrapper.success();
    }

    /**
     * 分页查询请款单的流转记录
     * @author 伍将
     * @date 2021/3/10
     * @param user: 当前登录用户
     * @param pageVO: 分页请求参数
     * @return 操作结果
     **/
    @Override
    public Wrapper<PageData<ApplyAmountOrderRecordVO>> pageList(UserLoginCacheDTO user, ApplyAmountOrderRecordPageVO pageVO) {

        // 组装查询条件
        Specification<ApplyAmountOrderRecordDO> spec =  (root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            list.add(criteriaBuilder.equal(root.get("applyId").as(Long.class), pageVO.getApplyId()));
            list.add(criteriaBuilder.equal(root.get("memberId").as(Long.class), user.getMemberId()));
            list.add(criteriaBuilder.equal(root.get("roleId").as(Long.class), user.getMemberRoleId()));

            Predicate[] p = new Predicate[list.size()];
            return criteriaBuilder.and(list.toArray(p));
        };

        // 组装分页参数
        Pageable page = PageRequest.of(pageVO.getCurrent() - 1, pageVO.getPageSize(), Sort.by("operateTime").ascending());

        Page<ApplyAmountOrderRecordDO> recordList = recordRepository.findAll(spec, page);
        List<ApplyAmountOrderRecordVO> resultList = recordList.stream().map(r -> {
            ApplyAmountOrderRecordVO entity = new ApplyAmountOrderRecordVO();
            entity.setOperator(r.getOperator());
            entity.setDepartment(r.getDepartment());
            entity.setJobTitle(r.getJobTitle());
            entity.setStatus(r.getStatus());
            entity.setStatusName(r.getStatusName());
            entity.setOperate(r.getOperate());
            entity.setOperateTime(DateUtil.format(DateUtil.date(r.getOperateTime()), "yyyy-MM-dd HH:mm"));
            entity.setOpinion(r.getOpinion());
            return entity;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(recordList.getTotalElements(), resultList));
    }

    /**
     * 删除流转记录
     * @author 伍将
     * @date 2021/3/10
     * @param applyId: 请款单id
     * @return 操作结果
     **/
    @Override
    public Wrapper<Void> delete(Long applyId) {

        recordRepository.deleteAllByApplyId(applyId);

        return Wrapper.success();
    }
}
